package lru;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-07-06 09:03
 * @version: 1.0
 */
public class Lru1 extends LinkedHashMap<Integer,Integer> {
    private int capacity;
    public Lru1(int capacity) {
        /** true表示替换并调整顺序*/
        super(capacity, 0.75f, true);
        this.capacity=capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return this.size()>capacity;
    }

    public int get(int key) {
       return super.get(key);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    public static void main(String[] args) {
        Lru1 lru1 = new Lru1(3);
        lru1.put(1, 1);
        lru1.put(2, 1);
        lru1.put(3, 1);

        lru1.put(4, 1);
        lru1.put(2, 1);

        int i = lru1.get(3);
        System.out.println(i);

        System.out.println(lru1.keySet());
    }
}
